<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>
	.KEYW {color: #933;}
	.COMM {color: #bbb; font-style: italic;}
	.NUMB {color: #393;}
	.STRN {color: #393;}
	.REGX {color: #339;}
	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
	</style></head><body><pre><span class='line'>  1</span> <span class="COMM">/**
<span class='line'>  2</span>  * 日期工具类
<span class='line'>  3</span>  * @class
<span class='line'>  4</span>  */</span><span class="WHIT">
<span class='line'>  5</span> </span><span class="NAME">FDLib.date</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>  6</span> </span><span class="WHIT">	</span><span class="COMM">/**
<span class='line'>  7</span> 	 * 判断是否是闰年
<span class='line'>  8</span> 	 * @param year 年份
<span class='line'>  9</span> 	 * @return 闰年为true
<span class='line'> 10</span> 	 */</span><span class="WHIT">
<span class='line'> 11</span> </span><span class="WHIT">	</span><span class="NAME">isLeapYear</span><span class="PUNC">:</span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">year</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 12</span> </span><span class="WHIT">		</span><span class="KEYW">return</span><span class="WHIT"> </span><span class="PUNC">!</span><span class="PUNC">!</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">year</span><span class="WHIT"> </span><span class="PUNC">&</span><span class="WHIT"> </span><span class="NUMB">3</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">year</span><span class="WHIT"> </span><span class="PUNC">%</span><span class="WHIT"> </span><span class="NUMB">100</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">year</span><span class="WHIT"> </span><span class="PUNC">%</span><span class="WHIT"> </span><span class="NUMB">400</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="NAME">year</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 13</span> </span><span class="WHIT">	</span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 14</span> </span><span class="WHIT">	</span><span class="COMM">/**
<span class='line'> 15</span> 	 * 是否是日期类型
<span class='line'> 16</span> 	 * @param dateStr 字符串日期
<span class='line'> 17</span> 	 * @return 是,true
<span class='line'> 18</span> 	 */</span><span class="WHIT">
<span class='line'> 19</span> </span><span class="WHIT">	</span><span class="PUNC">,</span><span class="NAME">isDateStr</span><span class="PUNC">:</span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">dateStr</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 20</span> </span><span class="WHIT">		</span><span class="KEYW">return</span><span class="WHIT"> </span><span class="PUNC">!</span><span class="PUNC">!</span><span class="NAME">this._parseDateStrToNum</span><span class="PUNC">(</span><span class="NAME">dateStr</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 21</span> </span><span class="WHIT">	</span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 22</span> </span><span class="WHIT">	</span><span class="COMM">/**
<span class='line'> 23</span> 	 * 根据年份获取每月的天数
<span class='line'> 24</span> 	 * @param year 年份
<span class='line'> 25</span> 	 * @return 返回每个月的天数,数组形式
<span class='line'> 26</span> 	 */</span><span class="WHIT">
<span class='line'> 27</span> </span><span class="WHIT">	</span><span class="PUNC">,</span><span class="NAME">getMonthDaysArr</span><span class="PUNC">:</span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">year</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 28</span> </span><span class="WHIT">		</span><span class="COMM">// 存放每月天数</span><span class="WHIT">
<span class='line'> 29</span> </span><span class="WHIT">		</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">month_days_arr</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="NUMB">31</span><span class="PUNC">,</span><span class="NUMB">28</span><span class="PUNC">,</span><span class="NUMB">31</span><span class="PUNC">,</span><span class="NUMB">30</span><span class="PUNC">,</span><span class="NUMB">31</span><span class="PUNC">,</span><span class="NUMB">30</span><span class="PUNC">,</span><span class="NUMB">31</span><span class="PUNC">,</span><span class="NUMB">31</span><span class="PUNC">,</span><span class="NUMB">30</span><span class="PUNC">,</span><span class="NUMB">31</span><span class="PUNC">,</span><span class="NUMB">30</span><span class="PUNC">,</span><span class="NUMB">31</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 30</span> </span><span class="WHIT">		</span><span class="COMM">// 二月份天数</span><span class="WHIT">
<span class='line'> 31</span> </span><span class="WHIT">		</span><span class="NAME">month_days_arr</span><span class="PUNC">[</span><span class="NUMB">1</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.isLeapYear</span><span class="PUNC">(</span><span class="NAME">year</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">?</span><span class="WHIT"> </span><span class="NUMB">29</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NUMB">28</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 32</span> </span><span class="WHIT">		</span><span class="WHIT">
<span class='line'> 33</span> </span><span class="WHIT">		</span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">month_days_arr</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 34</span> </span><span class="WHIT">	</span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 35</span> </span><span class="WHIT">	</span><span class="COMM">/**
<span class='line'> 36</span> 	 * 根据年月得到该月的最后一天
<span class='line'> 37</span> 	 * @param year 年份
<span class='line'> 38</span> 	 * @param month 月份
<span class='line'> 39</span> 	 * @return 返回该月最后一天
<span class='line'> 40</span> 	 */</span><span class="WHIT">
<span class='line'> 41</span> </span><span class="WHIT">	</span><span class="PUNC">,</span><span class="NAME">getEndDate</span><span class="PUNC">:</span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">year</span><span class="PUNC">,</span><span class="NAME">month</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 42</span> </span><span class="WHIT">		</span><span class="COMM">// 存放每月天数</span><span class="WHIT">
<span class='line'> 43</span> </span><span class="WHIT">		</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">month_days_arr</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.getMonthDaysArr</span><span class="PUNC">(</span><span class="NAME">year</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 44</span> </span><span class="WHIT">		</span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">month_days_arr</span><span class="PUNC">[</span><span class="NAME">month</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 45</span> </span><span class="WHIT">	</span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 46</span> </span><span class="WHIT">	</span><span class="COMM">/**
<span class='line'> 47</span> 	 * 将字符串日期转换成Date类型
<span class='line'> 48</span> 	 * 	
<span class='line'> 49</span> 	 * @param dateStr 字符串日期,如:
<span class='line'> 50</span> 	 *  var s = "2012-9-3 09:41:30";
<span class='line'> 51</span> 	 *  var s2 = "2012-09-03 09:41:30";
<span class='line'> 52</span> 	 *  var s3 = "9/3/2012 09:41:30";
<span class='line'> 53</span> 	 *  var s4 = "2012-9-3";
<span class='line'> 54</span> 	 * @return Date类型日期
<span class='line'> 55</span> 	 */</span><span class="WHIT">
<span class='line'> 56</span> </span><span class="WHIT">	</span><span class="PUNC">,</span><span class="NAME">parse</span><span class="PUNC">:</span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">dateStr</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 57</span> </span><span class="WHIT">		</span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">new</span><span class="WHIT"> </span><span class="NAME">Date</span><span class="PUNC">(</span><span class="NAME">this._parseDateStrToNum</span><span class="PUNC">(</span><span class="NAME">dateStr</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'> 58</span> </span><span class="WHIT">	</span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 59</span> </span><span class="WHIT">	</span><span class="PUNC">,</span><span class="NAME">_parseDateStrToNum</span><span class="PUNC">:</span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">dateStr</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 60</span> </span><span class="WHIT">		</span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">Date.parse</span><span class="PUNC">(</span><span class="NAME">dateStr.replace</span><span class="PUNC">(</span><span class="REGX">/-/g</span><span class="PUNC">,</span><span class="STRN">"/"</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 61</span> </span><span class="WHIT">	</span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 62</span> </span><span class="WHIT">	</span><span class="COMM">/**
<span class='line'> 63</span> 	 * 格式化日期&lt;br>
<span class='line'> 64</span> 	 * 使用方法:
<span class='line'> 65</span> 	 * &lt;code>
<span class='line'> 66</span> 	 * var dateStr = FDLib.date.format(new Date(),'yyyy-MM-dd hh:mm:ss.S');
<span class='line'> 67</span> 	 * &lt;/code>
<span class='line'> 68</span> 	 * 
<span class='line'> 69</span> 	 * @param dateInstance Date实例
<span class='line'> 70</span> 	 * @param 格式化字符串,如"yyyy-MM-dd","yyyy-MM-dd hh:mm:ss.S"
<span class='line'> 71</span> 	 * 
<span class='line'> 72</span> 	 * @return 返回格式化后的字符串
<span class='line'> 73</span> 	 */</span><span class="WHIT">
<span class='line'> 74</span> </span><span class="WHIT">	</span><span class="PUNC">,</span><span class="NAME">format</span><span class="PUNC">:</span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">dateInstance</span><span class="PUNC">,</span><span class="NAME">pattern</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 75</span> </span><span class="WHIT">		</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">o</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">   </span><span class="WHIT">
<span class='line'> 76</span> </span><span class="WHIT">			</span><span class="STRN">"M+"</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">dateInstance.getMonth</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">+</span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT">                 </span><span class="COMM">//月份    </span><span class="WHIT">
<span class='line'> 77</span> </span><span class="WHIT">			</span><span class="STRN">"d+"</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">dateInstance.getDate</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT">                    </span><span class="COMM">//日    </span><span class="WHIT">
<span class='line'> 78</span> </span><span class="WHIT">			</span><span class="STRN">"h+"</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">dateInstance.getHours</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT">                   </span><span class="COMM">//小时    </span><span class="WHIT">
<span class='line'> 79</span> </span><span class="WHIT">			</span><span class="STRN">"m+"</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">dateInstance.getMinutes</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT">                 </span><span class="COMM">//分    </span><span class="WHIT">
<span class='line'> 80</span> </span><span class="WHIT">			</span><span class="STRN">"s+"</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">dateInstance.getSeconds</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT">                 </span><span class="COMM">//秒    </span><span class="WHIT">
<span class='line'> 81</span> </span><span class="WHIT">			</span><span class="STRN">"q+"</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">Math.floor</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">dateInstance.getMonth</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">+</span><span class="NUMB">3</span><span class="PUNC">)</span><span class="PUNC">/</span><span class="NUMB">3</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="COMM">//季度    </span><span class="WHIT">
<span class='line'> 82</span> </span><span class="WHIT">			</span><span class="STRN">"S"</span><span class="WHIT">  </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">dateInstance.getMilliseconds</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="WHIT">             </span><span class="COMM">//毫秒    </span><span class="WHIT">
<span class='line'> 83</span> </span><span class="WHIT">		</span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">   </span><span class="WHIT">
<span class='line'> 84</span> </span><span class="WHIT">		</span><span class="KEYW">if</span><span class="PUNC">(</span><span class="REGX">/(y+)/</span><span class="PUNC">.</span><span class="NAME">test</span><span class="PUNC">(</span><span class="NAME">pattern</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 85</span> </span><span class="WHIT">			</span><span class="NAME">pattern</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">pattern.replace</span><span class="PUNC">(</span><span class="NAME">RegExp.$1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">dateInstance.getFullYear</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">+</span><span class="STRN">""</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">substring</span><span class="PUNC">(</span><span class="NUMB">4</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">RegExp.$1.length</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">   </span><span class="WHIT">
<span class='line'> 86</span> </span><span class="WHIT">		</span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 87</span> </span><span class="WHIT">			</span><span class="WHIT">
<span class='line'> 88</span> </span><span class="WHIT">		</span><span class="KEYW">for</span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">k</span><span class="WHIT"> </span><span class="KEYW">in</span><span class="WHIT"> </span><span class="NAME">o</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 89</span> </span><span class="WHIT">			</span><span class="KEYW">if</span><span class="PUNC">(</span><span class="KEYW">new</span><span class="WHIT"> </span><span class="NAME">RegExp</span><span class="PUNC">(</span><span class="STRN">"("</span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">k</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="STRN">")"</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">test</span><span class="PUNC">(</span><span class="NAME">pattern</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 90</span> </span><span class="WHIT">				</span><span class="NAME">pattern</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">pattern.replace</span><span class="PUNC">(</span><span class="NAME">RegExp.$1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">RegExp.$1.length</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'> 91</span> </span><span class="WHIT">					</span><span class="PUNC">?</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">o</span><span class="PUNC">[</span><span class="NAME">k</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'> 92</span> </span><span class="WHIT">					</span><span class="PUNC">:</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">(</span><span class="STRN">"00"</span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">o</span><span class="PUNC">[</span><span class="NAME">k</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">substring</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="STRN">""</span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">o</span><span class="PUNC">[</span><span class="NAME">k</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">length</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'> 93</span> </span><span class="WHIT">			</span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 94</span> </span><span class="WHIT">		</span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 95</span> </span><span class="WHIT">			</span><span class="WHIT">
<span class='line'> 96</span> </span><span class="WHIT">		</span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">pattern</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'> 97</span> </span><span class="WHIT">	</span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 98</span> </span><span class="PUNC">}</span><span class="PUNC">;</span></pre></body></html>